<?php
/*
 * @Author: your name
 * @Date: 2020-11-16 17:40:26
 * @LastEditTime: 2020-12-24 11:00:22
 * @LastEditors: Please set LastEditors
 * @Description: 主机列表验证器
 * @FilePath: /special/application/adminapiv3/validate/HostValidate.php
 */

namespace app\adminapiv3\validate;


use think\Validate;

class HostValidate extends Validate
{
    //验证规则
    protected $rule = [
        //主机ID
        'id|资产ID' => 'require|integer|length:1,10',
        //资产名
        'name|资产名称' => 'require|max:30',
        //主机系统
        'os|系统类别' => 'in:0,1,2',
        'tag|功能类别' => 'length:1,100',
        //所属业务系统ID
        'software_id|业务系统类别' => 'integer|length:1,10',
        //组织ID
        'ministry_id|所属部门' => 'integer|length:1,10',
        //安全管理员ID
        'manager_id|安全责任人' => 'integer|length:1,10',
        //开发商服务ID
        'develop_provider_id|系统开发商' => 'integer|length:1,10',
        // 运维服务商ID
        'maintain_provider_id|运维服务商' => 'integer|length:1,10',
        // 安全服务商ID
        'safe_provider_id|安全服务商' => 'integer|length:1,10',
        //资产位置ID
        'address_id|资产位置' => 'integer|length:1,10',
        // 主机IP地址
        'ip|IP地址' => 'require|ip',
        // 客户端安装方式 1手动 2自动
        'type|通信模式' => 'require|in:1,2|checkSSH',
        // SSH登录用户名
        'user|SSH登录用户名' => 'length:1,100',
        // SSH登录用户密码
        'pwd|SSH登录用户密码' => 'length:1,100',
        // SSH登录端口
        'port|SSH登录端口' => 'number|between:0,65535',
        // 所属管理组ID
        'group_id|所属管理组ID' => 'require|integer|length:1,10',
        // 主机ID
        'host_id|主机ID' => 'require|regex:([0-9]+[,]?)+',

        'is_recovery|是否恢复' => 'require|in:0,1',
    ];

    //错误信息
    protected $message = [
        'type.in' => '客户端安装方式格式错误'
    ];

    //验证场景
    protected $scene = [
        //添加
        'add' => [
            'name','tag','software_id','ministry_id','manager_id',
            'develop_provider_id','maintain_provider_id','safe_provider_id',
            'address_id','ip','type','user','pwd','port'
        ],

        //修改
        'modify' => [
            'id','name','tag','software_id','ministry_id','manager_id',
            'develop_provider_id','maintain_provider_id','safe_provider_id',
            'address_id'
        ],

        //删除
        'delete' => ['host_id', 'is_recovery'],

        //ssh
        'ssh' => ['ip', 'type', 'user','password','port'],

        //绑定资产组
        'bindingGroup' => ['group_id','host_id'],
        //网卡信息
        'network' => ['host_id'],
    ];

    /**
     * @name 验证ssh
     * @param $value
     * @param $rule
     * @param array $data
     * @return string
     * @author wx
     * @date 2020/12/17 15:09
     */
    protected function checkSSH($value, $rule, $data = [])
    {
        if($data['type'] == 2){
            if(!$data['user']){
                return 'SSH用户名不能为空';
            }
            if(!$data['pwd']){
                return 'SSH密码不能为空';
            }
            if(!$data['port']){
                return 'SSH端口不能为空';
            }
        }
        return true;
    }


}